package com.awesome.blog.controller;

import com.awesome.blog.entity.Article;
import com.awesome.blog.service.ArticleService;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
@CrossOrigin
public class ArticleController {
    @Autowired
    public ArticleService articleService;
    @ResponseBody
    @RequestMapping(value="/selectByType/{variable}",produces="text/json;charset=UTF-8",method=RequestMethod.POST )
    private String selectByType(@PathVariable String variable){
        //System.out.println(variable);
        List<Article> list = articleService.selectByType(variable);
        //判断list是否为空
        if(list!=null && !list.isEmpty()){
            Gson gson=new Gson();
            String gson1=gson.toJson(list);
            return gson1;
        }else{
            return "{\"result\":\"[]\"}";
        }
    }
//    RequestBody使用json数据
    @ResponseBody
    @RequestMapping(value="/selectShareData",produces="text/json;charset=UTF-8",method=RequestMethod.POST )
    private String selectShareData(@RequestBody String param){
        //System.out.println(param);
        JsonParser jsonParser = new JsonParser();
        JsonObject jsonObject = jsonParser.parse(param).getAsJsonObject();
        String type = jsonObject.get("type").getAsString();
        List<Article> list = articleService.selectByType(type);
        if(list!=null && !list.isEmpty()){
            Gson gson=new Gson();
            String gson1=gson.toJson(list);
            return gson1;
        }else{
            return "{\"result\":\"[]\"}";
        }
    }
    @ResponseBody
    @RequestMapping(value="/selectByCate/{variable}",produces="text/json;charset=UTF-8",method=RequestMethod.POST )
    private String selectByCate(@PathVariable String variable){
        List<Article> list = articleService.selectByCate(variable);
        if(list!=null && !list.isEmpty()){
            Gson gson=new Gson();
            String gson1=gson.toJson(list);
            return gson1;
        }else{
            return "{\"result\":\"[]\"}";
        }
    }
    @ResponseBody
    @RequestMapping(value="/selectByTime/{variable}",produces="text/json;charset=UTF-8",method=RequestMethod.POST )
    private String selectByTime(@PathVariable String variable){
        List<Article> list = articleService.selectByTime(variable);
        if(list!=null && !list.isEmpty()){
            Gson gson=new Gson();
            String gson1=gson.toJson(list);
            return gson1;
        }else{
            return "{\"result\":\"[]\"}";
        }
    }
    @ResponseBody
    @RequestMapping(value="/selectBySearch/{variable}",produces="text/json;charset=UTF-8",method=RequestMethod.POST )
    private String selectBySearch(@PathVariable String variable){
        List<Article> list = articleService.selectBySearch(variable);
        if(list!=null && !list.isEmpty()){
            Gson gson=new Gson();
            String gson1=gson.toJson(list);
            return gson1;
        }else{
            return "{\"result\":\"[]\"}";
        }
    }
    @ResponseBody
    @RequestMapping(value="/selectById/{variable}",produces="text/json;charset=UTF-8",method=RequestMethod.POST )
    private String selectById(@PathVariable Integer variable){
        System.out.println(variable);
        Article article = articleService.selectById(variable);
        Gson gson=new Gson();
        String gson1=gson.toJson(article);
        return gson1;
    }
}
